package game.core.objects
{
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;
	
	import game.core.BallFactory;
	import game.core.Config;
	import game.core.GameObject;
	import game.core.GameSystem;
	import game.core.effect.BallEffect;
	import game.core.util.DisplayUtil;
	import game.event.GameEvent;
	
	public class Ball extends GameObject
	{
		private var _vx:Number;
		
		private var _vy:Number;
		
		public var bounce:Number = -1;
		
		public var gravity:Number = .5;
		
		private var _radius:Number = 45;
		
		public function set radius(value:Number):void {
			if(this._radius != value){
				this._radius = value;
				this.scaleX = this.scaleY = value / 45;
			}
		}
		
		public function get radius():Number {
			return this._radius;
		}
		
		
		public static const MIN_RADIUS:Number = 10;
		
		public function set vx(value:Number):void {
			this._vx = value;
		}
		
		public function get vx():Number {
			return this._vx;
		}
		
		public function set vy(value:Number):void {
			this._vy = value;
		}
		
		public function get vy():Number {
			return this._vy;
		}
		
		public function Ball()
		{
			super();
		}
		
		/**
		 * 初始化的一个自由下落的过程
		 */ 
		public function dropAction():void {
			this.addEventListener(Event.ENTER_FRAME,dropActionEnterFrame);
		}
		
		private function dropActionEnterFrame(event:Event):void {
			vy += 0.1;
			this.y += vy;
			if(this.y > 60){// 这里还需要配置一下
				this.removeEventListener(Event.ENTER_FRAME,dropActionEnterFrame);
				this.pause = false;
			}
		}
		
		override public function set pause(value:Boolean):void {
			super.pause = value;
			if(pause){
				this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			}else {
				if(!this.hasEventListener(Event.ENTER_FRAME)){
					this.addEventListener(Event.ENTER_FRAME,onEnterFrame);// 需要判断一下,是否停止了,如果停止了,需要不能进行了
				}
			}
		}
		
		override protected function createChildren():void {
			super.createChildren();
			var skin:*;
			/* CONFIG::debug {
				skin = new Sprite();
				var g:Graphics = skin.graphics;
				g.clear();
				g.beginFill(Math.random() * 0xFFFFFF,1);
				g.drawCircle(0,0,this.radius);
				g.endFill();
			}
			CONFIG::release {
				var cls:Class = flash.utils.getDefinitionByName(this.data.className) as Class;
				skin = new cls();
			} */
			var className:String = DisplayUtil.getQualifiedClassName(this) + Config.SKIN;
			var cls:Class = flash.utils.getDefinitionByName(className) as Class;
			skin = new cls();
			this.addChild(skin);
		}
		
		/**
		 * 小球运动的过程
		 * 暂时先写死了,bounds是来检测内容的
		 * 这里的算法,还是需要调整一下
		 */ 
		private function onEnterFrame(event:Event):void
		{
			if(originalGravityEnabled){
				gravity += 0.1;
				if(gravity == originalGravity){// 需要保证这个值的大小
					originalGravityEnabled = false;
				}
			} 
			vy += gravity;
			this.x += vx;
			this.y += vy;
			var left:Number = this.bounds.x;
			var right:Number = this.bounds.width;
			var top:Number = this.bounds.y;
			var bottom:Number = this.bounds.height;
			if(this.x + this.radius > right)
			{
				this.x = right - this.radius;
				vx *= bounce;
			}
			else if(this.x - this.radius < left)
			{
				this.x = left + this.radius;
				vx *= bounce;
			}
			if(this.y + this.radius > bottom)
			{
				this.y = bottom - this.radius;
				vy *= bounce;
				trace("run acb");
			}
			else if(this.y - this.radius < top)
			{
				vy *= bounce;
				this.y = top + this.radius;
			}
		}
		
		private var _bounds:Rectangle;// 边界
		
		public function set bounds(value:Rectangle):void {
			this._bounds = value;
		}
		
		/**
		 * 这里的设计可能还是有一些问题的
		 */ 
		public function get bounds():Rectangle {
			if(!this._bounds){
				CONFIG::debug {
					this._bounds = new Rectangle(this.contentX,this.contentY,this.contentWidth,this.contentHeight);
				}
			}
			return this._bounds;
		}
		
		private var _contentX:Number;
		
		public function get contentX():Number {
			if(!this._contentX){
				this._contentX = -this.radius;
			}
			return this._contentX;
		}
		
		private var _contentY:Number;
		
		public function get contentY():Number {
			if(!this._contentY){
				this._contentY = 0;
			}
			return this._contentY;
		}
		
		private var _contentWidth:Number;
		
		public function get contentWidth():Number {
			if(!this._contentWidth){
				this._contentWidth = GameSystem.instance.contentWidth - this.radius;
			}
			return this._contentWidth;
		}
		
		private var _contentHeight:Number;
		
		public function get contentHeight():Number {
			if(!this._contentHeight){
				this._contentHeight = GameSystem.instance.conentHeight - this.radius;
			}
			return this._contentHeight;
		}
		
		/**
		 * 小球分裂
		 * 分裂时,需要分成2个小时的
		 * 左右,中间还需要有一个爆炸的动画,暂时先不处理
		 * 分裂时y值的位置,需要调整
		 * 另外需要注意分裂时,y的位置是向上跑的然后到一定的高度,在向下跑
		 */ 
		
		public var originalGravityEnabled:Boolean;// 是否支持使用原始的重力
		public var originalGravity:Number;// 原始的重力,因为移动时,可能有一个向上的过程,并且保证Y值在一定的高度	 
		override public function execute():void {
			if(this.splitabled){
				var halfRadius:Number = this.radius / 2;
				var yy:Number = this.y + (this.radius - halfRadius) / 2;
				var leftBall:Ball = BallFactory.randomPauseBall(halfRadius,this.gName);
				leftBall.radius = halfRadius;
				leftBall.x = this.x;
				leftBall.y = yy;
				leftBall.vx = 5;
				leftBall.vy = -3;
				leftBall.gravity = -(this.gravity + 0.1);
				leftBall.pause = GameSystem.instance.isPause;
				leftBall.gName = this.gName;
				leftBall.originalGravityEnabled = true;
				leftBall.originalGravity = this.gravity;
				GameSystem.instance.addGameObject(leftBall);
				var rightBall:Ball = BallFactory.randomPauseBall(this.radius,this.gName);
				rightBall.radius = halfRadius;
				rightBall.x = this.x + this.radius;
				rightBall.y = yy;
				rightBall.vx = -5;
				rightBall.vy = -3;
				rightBall.gravity = -(this.gravity + 0.1);
				rightBall.pause = GameSystem.instance.isPause;
				rightBall.originalGravityEnabled = true;
				rightBall.originalGravity = this.gravity;
				rightBall.gName = this.gName;
				GameSystem.instance.addGameObject(rightBall);
				var effect:BallEffect = new BallEffect(new ExplodeEffect());
				effect.x = this.x + this.width / 2;
				effect.y = this.y + this.height / 2;
				effect.scaleX = effect.scaleY = this.scaleX;
				GameSystem.instance.addChild(effect);
			}
			CONFIG::debug {
				this.data = 100;
			}
			var gameEvent:GameEvent = new GameEvent(GameEvent.SCORE_EVENT,true);
			gameEvent.data = this.data;
			this.dispatchEvent(gameEvent);// 发送一个事件,冒泡,到时记得停止,去GameSystem里处理
			GameSystem.instance.removeGameObject(this);
		}
		
		/**
		 * 是否可以分裂 当半径小于一定程度时,就不可以分裂了
		 */ 
		public function get splitabled():Boolean {
			if(this.radius <= MIN_RADIUS){
				return false;
			}else {
				return true;
			}
		}
		
		override public function dispose():void {
			super.dispose();
			this.pause = true;
			this.bounds = null;
		}
	}
}